package cn.yto.print.plugins;

import cn.yto.print.config.ThreadPoolManager;
import cn.yto.print.model.request.PrintTaskRequest;
import org.springframework.stereotype.Component;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executor;
import java.util.concurrent.LinkedBlockingQueue;

@Component
public class PrintResourceFactory {

    private final LinkedBlockingQueue<PrintTaskRequest> globalTaskQueue = new LinkedBlockingQueue<>();
    private final ConcurrentHashMap<String, LinkedBlockingQueue<PrintTaskRequest>> printerTaskQueues = new ConcurrentHashMap<>();
    private final ConcurrentHashMap<String, Executor> printerExecutors = new ConcurrentHashMap<>();

    public LinkedBlockingQueue<PrintTaskRequest> getGlobalTaskQueue() {
        return globalTaskQueue;
    }

    public LinkedBlockingQueue<PrintTaskRequest> getPrinterQueue(String printerName) {
        return printerTaskQueues.computeIfAbsent(printerName, k -> new LinkedBlockingQueue<>());
    }

    public Executor getPrinterExecutor(String printerName) {
        return printerExecutors.computeIfAbsent(printerName, k -> ThreadPoolManager.getExecutorService());
    }

}
